﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Collections.Specialized;
using System.IO;
using System.Text.RegularExpressions;
namespace bezlimitu.App_Code
{
    public class TurbobitAgent : Agent
    {
        private static string LoginUrl = "http://turbobit.net/login";
        private static string LogoutUrl = "http://turbobit.net/user/logout";
        protected String Url { get; set; }
        protected String Username { get; set; }
        protected String Password { get; set; }
        protected String DirectUrl { get; set; }
        public String ResponseLogin { get; set; }
        protected CookieCollection cookie;
        protected long FileSize { get; set; }
        protected String FileName { get; set; }
        public TurbobitAgent(String Url, String Username, String Password)
        {
            this.Url = Url;
            this.Username = Username;
            this.Password = Password;
            this.DirectUrl = String.Empty;
            this.FileSize = 0;
            cookie = new CookieCollection();
        }

        public TurbobitAgent(String Url)
        {
            this.Url = Url;
            this.DirectUrl = String.Empty;
            this.FileSize = 0;
        }

        public override bool LogIn()
        {
            string postData = "user%5Blogin%5D=sandy_csh%40hotmail.com&user%5Bpass%5D=2689gazetto&user%5Bsubmit%5D=Login";

            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(LoginUrl);
            webRequest.Method = "GET";
            webRequest.Headers.Add("Accept-Language", "en-EN");
            webRequest.CookieContainer = new CookieContainer();
            HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
            cookie.Add(response.Cookies);

            webRequest = (HttpWebRequest)HttpWebRequest.Create("http://turbobit.net/user/login");
            webRequest.Headers.Add("Accept-Language", "en-EN");
            webRequest.CookieContainer = new CookieContainer();
            webRequest.CookieContainer.Add(cookie);
            webRequest.Method = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.ContentLength = postData.Length;
            webRequest.AllowAutoRedirect = false;
            using (StreamWriter requestWriter2 = new StreamWriter(webRequest.GetRequestStream()))
            {
                requestWriter2.Write(postData);
            }
            response = (HttpWebResponse)webRequest.GetResponse();
            string responseData = string.Empty;

            using (StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
            {
                responseData = responseReader.ReadToEnd();
            }
            cookie.Add(response.Cookies);
            foreach (Cookie c in cookie)
            {
                System.Diagnostics.Debug.WriteLine(c.Name + "=" + c.Value);
                if (c.Name.Equals("sid"))
                {
                    return true;
                }
            }
            return false;
        }
        public override bool LogOut()
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(LogoutUrl);
            webRequest.CookieContainer = new CookieContainer();
            webRequest.CookieContainer.Add(cookie);
            return true;
        }

        public override DownloadLink GetFileInfo()
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Url);
            webRequest.CookieContainer = new CookieContainer();
            webRequest.CookieContainer.Add(cookie);
            webRequest.AllowAutoRedirect = true;
            
            HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
            string responseData;
            using (StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
            {
                responseData = responseReader.ReadToEnd();
            }
            Match match = Regex.Match(responseData, @"http://[^/]+//download/redirect/[^']*", RegexOptions.IgnoreCase);
            if (match.Success)
            {
                webRequest = (HttpWebRequest)WebRequest.Create(match.Groups[0].Value);
                webRequest.CookieContainer = new CookieContainer();
                webRequest.CookieContainer.Add(cookie);

                response = (HttpWebResponse)webRequest.GetResponse();

                DownloadLink url = new DownloadLink(response.ResponseUri.OriginalString);
                url.Length = response.ContentLength;
                url.Name = new Uri(Url).Segments[new Uri(Url).Segments.Length-1].Replace(".html","").Replace(".htm","");
                return url;
            }
            DownloadLink ur2l = new DownloadLink("");
            ur2l.Length = -1;
            ur2l.Name = response.ResponseUri.Segments[response.ResponseUri.Segments.Length - 1];
            return ur2l;
        }
    }
}